package com.xuqi.Game;

import java.util.ArrayList;

import com.xuqi.Game.GameElement.ELoaction;
import com.xuqi.Game.GameElement.ESize;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;

public class GameElemShape extends GameElement
{
	private static final String TAG = "GameElemShape";

	public GameElemShape()
	{
		super();
		Log.d(TAG, "GameElemShape()");
		this.m_ElementArray.add(new GameElement());
		this.SetShape(new byte[][]{{0}});
		this.SetElemLoc(new ELoaction(0, 0));
	}

	public GameElemShape(SLoaction _location)
	{
		super();
		Log.d(TAG, "GameElemShape(SLoaction _location)");
		this.m_ElementArray.add(new GameElement());
		this.SetShape(new byte[][]{{0}});
		this.m_cShapeLocation = _location;
	}
	
	public GameElemShape(ArrayList<GameDrawElemIF> m_aArrayList,byte[][] nShape,SLoaction _location)
	{
		super();
		Log.d(TAG, "GameElemShape(ArrayList<GameElement> m_aArrayList,int [][] nShape,SLoaction _location)");
		this.SetElemArray(m_aArrayList);
		this.SetShape(nShape);
		this.m_cShapeLocation = _location;
	}

	public GameDrawElemIF MoveUpDown(int newTop)
	{
		this.m_cShapeLocation.top = newTop;
		return this;
	}
	public GameDrawElemIF MoveLeftRight(int newLeft)
	{
		this.m_cShapeLocation.left = newLeft;
		return this;
	}
	@Override
	public GameDrawElemIF SetLocation(int left, int top)
	{
		this.m_cShapeLocation.left = left;
		this.m_cShapeLocation.top = top;
		return this;
	}
	@Override
	public GameDrawElemIF SetSize(int nWidth, int nHeight)
	{
		// TODO Auto-generated method stub
		this.m_cShapeSize.width = nWidth;
		this.m_cShapeSize.height = nHeight;
		return this;
	}

	public GameDrawElemIF SetElemArray(ArrayList<GameDrawElemIF> m_aArrayList)
	{
		this.m_ElementArray = m_aArrayList;
		return this;
	}
	
	public GameDrawElemIF SetShape(byte[][] js)
	{
		m_Shape = js;
		//cal height
		m_hCount = m_Shape.length;
		//cal width
		for(int i = 0; i < m_hCount; i++)
		{
			if(m_wCount < m_Shape[i].length)
			{
				m_wCount = m_Shape[i].length;
				
			}
		}
		Log.d(TAG, "m_hCount:" +m_hCount +"\t m_wCount:" + m_wCount);
		return this;
	}
	
	@Override
	public void GDraw(Canvas _canvas)
	{
		Log.d(TAG, "GDraw");
		this.DrawShape(_canvas);
	}

	
	public void DrawShape(Canvas _canvas)
	{
		GameDrawElemIF element = null;
		for (int y = 0; y < m_hCount; y++)
		{
			for (int x = 0; x < m_Shape[y].length; x++)
			{
				ELoaction shapeelLoaction = S2ELoaction(this.m_cShapeLocation);
				Log.d(TAG, "Shape ELoc X:" + shapeelLoaction.left + "\tY:"+ shapeelLoaction.top);
				Log.d(TAG, "DrawShape X:" + x + "\tY:" + y);
				if(m_Shape[y][x] >= 0 
						&& m_ElementArray != null 
						&& m_ElementArray.size() >= m_Shape[y][x])
				{
					element = m_ElementArray.get(m_Shape[y][x]);
					((GameElement) element).SetElemLoc(shapeelLoaction.add(S2ELoaction(new SLoaction(x, y))));
				}
				
				if (element != null)
				{
					element.GDraw(_canvas);
				}
			}
		}

	}

	@Override
	public GameDrawElemIF SetElemSize(ESize eSize)
	{
		// TODO Auto-generated method stub
		for(int i = 0; i < m_ElementArray.size(); i++)
		{
			SetElemSizeByIndex(eSize,i);
		}
		return this;
	}

	public GameDrawElemIF SetElemSizeByIndex(ESize eSize,int nIndex)
	{
		if(nIndex < m_ElementArray.size())
		{
			m_ElementArray.get(nIndex).SetSize(eSize.width, eSize.height);
			this.m_cElemSize = eSize;
			return m_ElementArray.get(nIndex);
		}
		else
		{
			return null;
		}
		
	}
	
	public ESize S2ESize(int w, int h)
	{
		return S2ESize(new SSize(w, h));
	}

	public ELoaction S2ELoaction(int left, int top)
	{
		return S2ELoaction(new SLoaction(left, top));
	}

	public SSize E2SSize(int w, int h)
	{
		return E2SSize(new ESize(w, h));
	}

	public SLoaction E2SLoaction(int left, int top)
	{
		return E2SLoaction(new ELoaction(left, top));
	}

	public ESize S2ESize(SSize _size)
	{
		return new ESize(_size.width * this.m_cElemSize.width, _size.height * this.m_cElemSize.height);
	}

	public ELoaction S2ELoaction(SLoaction _loc)
	{
		return new ELoaction(_loc.left * this.m_cElemSize.width, _loc.top * this.m_cElemSize.height);
	}

	public SSize E2SSize(ESize _size)
	{
		return new SSize(_size.width / this.m_cElemSize.width, _size.height / this.m_cElemSize.height);
	}

	public SLoaction E2SLoaction(ELoaction _loc)
	{
		return new SLoaction(_loc.left / this.m_cElemSize.width, _loc.top / this.m_cElemSize.height);
	}

	/**********************************************/
	private int m_wCount = -1;
	private int m_hCount = -1;
	protected int m_cShapeColor = Color.WHITE;
	protected SSize m_cShapeSize = new SSize(m_wCount, m_hCount);
	protected SLoaction m_cShapeLocation = new SLoaction(0, 0);
	protected ArrayList<GameDrawElemIF> m_ElementArray = new ArrayList<GameDrawElemIF>();
	
	public byte[][] m_Shape = new byte[][]
	{
	{ 0 }};

	public static class SLoaction
	{
		public SLoaction(int _left, int _top)
		{
			super();
			this.left = _left;
			this.top = _top;
		}

		public SLoaction add(SLoaction loc)
		{
			this.left += loc.left;
			this.top += loc.top;
			return this;
		}

		public int left = 0;
		public int top = 0;
	}

	public static class SSize
	{

		public SSize(int _width, int _height)
		{
			super();
			this.width = _width;
			this.height = _height;
		}

		public int width = 0;
		public int height = 0;

	}

}
